package com.fastloan.mgt.verify.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastloan.mgt.customer.model.KXBankCardResult;
import com.fastloan.mgt.verify.enums.ApiStore;
import com.fastloan.mgt.verify.model.ApiResult;
import com.fastloan.mgt.verify.model.baidu.BaiDuApiResult;
import com.fastloan.mgt.verify.model.bankcard.BankCardVerifyParam;
import com.fastloan.mgt.verify.model.bankcard.BankCardVerifyResult;
import com.fastloan.mgt.verify.model.personbank.PersonBankParam;
import com.fastloan.mgt.verify.model.GeneralVerifyResult;
import com.fastloan.mgt.verify.model.tobacco.TobaccoICertificate;
import com.fastloan.mgt.verify.service.VerifyService;
import com.fastloan.mgt.verify.util.ApiUtil;
import com.fastloan.mgt.verify.util.GetParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.function.Consumer;

import static com.fastloan.mgt.verify.util.ApiUtil.KX_SUCCESS_CODE;

/**
 * 三方接口Service实现
 *
 * @author yanwe
 * createTime 2017-12-2017/12/8 10:04
 */
@Service
public class VerifyServiceImpl implements VerifyService {

    private static final Logger logger = LoggerFactory.getLogger(VerifyServiceImpl.class);

    @Override
    public BankCardVerifyResult verifyBankCard(BankCardVerifyParam bankCardVerifyParam) {
        ApiResult apiResult = ApiUtil.sendGetSync(GetParam.newBuilder()
                        .add("cardId", bankCardVerifyParam.getBankCardNo())
                        .add("name", bankCardVerifyParam.getName())
                        .add("personalId", bankCardVerifyParam.getIdentity())
                        .add("mobileNum", bankCardVerifyParam.getPhone())
                , ApiStore.BANK_CARD);
        if (!Objects.equals(apiResult.getRet(), KX_SUCCESS_CODE)){
            return BankCardVerifyResult.BankCardVerifyResultBuilder.aBankCardVerifyResult()
                    .withRet(apiResult.getRet()).withMsg(apiResult.getMsg()).build();
        }
        BaiDuApiResult baiDuApiResult = JSON.parseObject(apiResult.getData(), BaiDuApiResult.class);
        if (!Objects.equals(baiDuApiResult.getRetCode(), KX_SUCCESS_CODE)){
            return BankCardVerifyResult.BankCardVerifyResultBuilder.aBankCardVerifyResult()
                    .withRet(baiDuApiResult.getRetCode()).withMsg(baiDuApiResult.getRetMsg()).build();
        }
        String result = baiDuApiResult.getResult();
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject detail = jsonObject.getJSONObject("detail");
        BankCardVerifyResult bankCardVerifyResult = new BankCardVerifyResult();
        bankCardVerifyResult.setMsg(baiDuApiResult.getRetMsg());
        bankCardVerifyResult.setRet(baiDuApiResult.getRetCode());
        if (detail != null){
            JSONObject bankCardNo = detail.getJSONObject("bankCardNo");
            if (bankCardNo != null){
                bankCardVerifyResult.setBankCardNoStatus(bankCardNo.getInteger("status"));
            }
            JSONObject name = detail.getJSONObject("name");
            if (name != null){
                bankCardVerifyResult.setNameStatus(name.getInteger("status"));
            }
            JSONObject phone = detail.getJSONObject("phone");
            if (phone != null){
                bankCardVerifyResult.setPhoneStatus(phone.getInteger("status"));
            }
        }
        return bankCardVerifyResult;
    }

    @Override
    public GeneralVerifyResult personBankVerify(PersonBankParam personBankParam){
        ApiResult apiResult = ApiUtil.sendJsonSync(JSON.toJSONString(personBankParam),
                ApiStore.PERSON_BANK_VERIFY);
        if (!Objects.equals(apiResult.getRet(), KX_SUCCESS_CODE)){
            return GeneralVerifyResult.GeneralVerifyResultBuilder.aGeneralVerifyResult()
                    .withRet(apiResult.getRet()).withMsg(apiResult.getMsg()).withPass(false).build();
        }
        JSONObject jsonObject = JSON.parseObject(apiResult.getData());
        boolean isPass = Objects.equals(jsonObject.getInteger("status"), ApiUtil.SUCCESS_CODE) && jsonObject.getBoolean("data");
        return GeneralVerifyResult.GeneralVerifyResultBuilder.aGeneralVerifyResult()
                .withRet(apiResult.getRet()).withMsg(jsonObject.getString("message")).withPass(isPass).build();
    }

    @Override
    public GeneralVerifyResult tobaccoVerify(TobaccoICertificate tobaccoICertificate){
        ApiResult apiResult = ApiUtil.sendJsonSync(JSON.toJSONString(tobaccoICertificate), ApiStore.BBD_TOBACCO_VERIFY);
        if (!Objects.equals(apiResult.getRet(), KX_SUCCESS_CODE)){
            return GeneralVerifyResult.GeneralVerifyResultBuilder.aGeneralVerifyResult()
                    .withRet(apiResult.getRet()).withMsg(apiResult.getMsg()).withPass(false).build();
        }
        JSONObject jsonObject = JSON.parseObject(apiResult.getData());
        JSONObject data = jsonObject.getJSONObject("data");
        boolean isPass = Objects.equals(jsonObject.getInteger("status"), ApiUtil.SUCCESS_CODE) && data.getBoolean("isVerifySucc");
        return GeneralVerifyResult.GeneralVerifyResultBuilder.aGeneralVerifyResult()
                .withRet(apiResult.getRet()).withMsg(jsonObject.getString("message")).withPass(isPass)
                .withData(data.getString("verifyId")).build();
    }

    @Override
    public KXBankCardResult selectBankCardType(String cardNum){
        ApiResult apiResult = ApiUtil.sendGetSync(GetParam.newBuilder().add("cardId", cardNum)
                , ApiStore.BANK_CARD_TYPE);
        return apiResult.getData(KXBankCardResult.class);
    }

}
